using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteECSWeb;

public partial class UControls_EmployeeProfile : System.Web.UI.UserControl
{
    EcsEmployeeBLL EcsEpmloyeeAPI = null;
    static int EmpID = 0;
    string EmpQualfDescNoData;
    DateTime EmpQualfDateNoData;
    //int companyIDtable = 0;

    #region Props
    private string _empUserName = "";

    public string EmpUserName
    {
        get { return _empUserName; }
        set { _empUserName = value; }
    }

    public string EmpEmail
    {
        get { return txt_EmpEmail.TextBoxData; }
    }

    private string _validationGroup;

    public string ValidationGroup
    {
        get { return _validationGroup; }
        set { _validationGroup = value; }
    }

    #endregion

    #region OverrideControlMethods
    protected override void OnInit(EventArgs e)
    {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }
    protected override void LoadControlState(object savedState)
    {
        object[] ctlState = (object[])savedState;
        base.LoadControlState(ctlState[0]);
        _empUserName = ctlState[1].ToString();
    }
    protected override object SaveControlState()
    {
        object[] ctlState = new object[2];
        ctlState[0] = base.SaveControlState();
        ctlState[1] = _empUserName;
        return ctlState;
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (!string.IsNullOrEmpty(ValidationGroup))
            {
                Control x = new Control();
                txt_EmpNameAr.ValidationGroupText = ValidationGroup;
                ddlEmpGender.ValidationGroup = ValidationGroup;
                txt_EmpBirthDate.ValidationGroupText = ValidationGroup;
                txt_EmpAddressEg.ValidationGroupText = ValidationGroup;
                txt_EmpNameEn.ValidationGroupText = ValidationGroup;
                txt_EmpPhoneEg.ValidationGroupText = ValidationGroup;
                txt_EmpEmail.ValidationGroupText = ValidationGroup;
                txt_EmpEJion.ValidationGroupText = ValidationGroup;
                txt_EmpSJion.ValidationGroupText = ValidationGroup;
                txt_EmpPhoneOff.ValidationGroupText = ValidationGroup;
                txt_EmpMobileOff.ValidationGroupText = ValidationGroup;
                txt_EmpJionCode.ValidationGroupText = ValidationGroup;
                RequiredField.ValidationGroup = ValidationGroup;
            }
            if (this.EmpUserName.Length > 0)
            {
                MembershipUser mu = Membership.GetUser(EmpUserName);
                EcsEpmloyeeAPI = new EcsEmployeeBLL();
                EcsSiteDS.EcsEmployeeDataTable EcsEmpTable =
                    EcsEpmloyeeAPI.GetEcsEmployeeByEmpEmail(mu.Email);
                EcsSiteDS.EcsEmployeeRow EcsEmpRow = EcsEmpTable[0];

                txt_EmpNameAr.TextBoxData = EcsEmpRow.Ecsemp_nmar;

                ddlEmpGender.SelectedValue =
                    EcsEmpRow.IsEcsemp_genderNull() ? string.Empty : EcsEmpRow.Ecsemp_gender;
                txt_EmpBirthDate.TextBoxData =
                    EcsEmpRow.IsEcsemp_BirthDateNull() ? string.Empty :
                    EcsEmpRow.Ecsemp_BirthDate.ToString("dd/MM/yyyy");
                txt_EmpAddressEg.TextBoxData =
                    EcsEmpRow.IsEcsemp_addrsarEgNull() ? string.Empty : EcsEmpRow.Ecsemp_addrsarEg;
                
                txt_EmpNameEn.TextBoxData =
                    EcsEmpRow.IsEcsemp_nmenNull() ? string.Empty : EcsEmpRow.Ecsemp_nmen;
                
                txt_EmpPhoneEg.TextBoxData =
                    EcsEmpRow.IsEcsemp_phnEgNull() ? string.Empty : EcsEmpRow.Ecsemp_phnEg;

                txt_EmpEmail.TextBoxData =
                    EcsEmpRow.IsEcsemp_emailNull() ? string.Empty : EcsEmpRow.Ecsemp_email;

                txt_EmpEJion.TextBoxData =
                    EcsEmpRow.IsEcsemp_EJoinDateNull() ? string.Empty :
                    EcsEmpRow.Ecsemp_EJoinDate.ToString("dd/MM/yyyy");

                txt_EmpSJion.TextBoxData =
                    EcsEmpRow.IsEcsemp_SJoinDateNull() ? string.Empty :
                    EcsEmpRow.Ecsemp_SJoinDate.ToString("dd/MM/yyyy");

                txt_EmpJionCode.TextBoxData =

                    EcsEmpRow.IsEcsemp_JoinCodeNull() ? string.Empty :
                    Microsoft.VisualBasic.Strings.Right(EcsEmpRow.Ecsemp_JoinCode,
                    EcsEmpRow.Ecsemp_JoinCode.Length - 4);

                txt_EmpPhoneOff.TextBoxData =
                    EcsEmpRow.IsEcsemp_phnOffNull() ? string.Empty : EcsEmpRow.Ecsemp_phnOff;

                txt_EmpMobileOff.TextBoxData =
                    EcsEmpRow.IsEcsemp_mobilOffNull() ? string.Empty : EcsEmpRow.Ecsemp_mobilOff;

                ddlEmpTypes.SelectedValue = EcsEmpRow.EmpType_ID.ToString();
                EmpID = EcsEmpRow.Ecsemp_id;

                QualfDataSource.SelectParameters["Ecsemp_id"].DefaultValue =
                    EcsEmpRow.Ecsemp_id.ToString();
                QualfDataSource.UpdateParameters["Ecsemp_id"].DefaultValue =
                    EcsEmpRow.Ecsemp_id.ToString();

                if (EcsEmpRow.IsEcsemp_PicturepathNull())
                {
                    if (Session["UploadEmpImg"] != null)
                    {
                        imgPhoto.ImageUrl = ImageHelper.SetImgPhotoPath
                            ("UploadEmpImg",
                            Globals.Settings.UploadInfo.TempUploadFolder, PhotoSize.Small);
                    }
                }
                else
                {
                    imgPhoto.ImageUrl = ImageHelper.SetImgPhotoPath
                        (EcsEmpRow.IsEcsemp_PicturepathNull() ? string.Empty : EcsEmpRow.Ecsemp_Picturepath 
                        , null, PhotoSize.Small);
                }
                
                pnlEditView.Visible = true;
                pnlAddView.Visible = false;

            }
            else
            {
                pnlEditView.Visible = false;
                pnlAddView.Visible = true;
            }
        }
        Response.AddHeader("Refresh", Convert.ToString((Session.Timeout * 60) - 10));

        PopupHyperLink1.URL =
           SiteECSWeb.Helper.UploadUrlPath
           (Globals.Settings.UploadInfo.EmpImgAttachFolder, this.Request, "RefeshContent");
    }

    public int SaveEmpProfile()
    {

        EcsEpmloyeeAPI = new EcsEmployeeBLL();
        DateTime? EmpEJionDate = null;
        if (!string.IsNullOrEmpty(txt_EmpEJion.TextBoxData))
        {
            EmpEJionDate = DateTime.Parse(txt_EmpEJion.TextBoxData);
        }
        else
        {
            EmpEJionDate = null;
        }
        if (this.EmpUserName.Length > 0)
        {
            MembershipUser mu = Membership.GetUser(EmpUserName);
            EcsEpmloyeeAPI = new EcsEmployeeBLL();
            EcsSiteDS.EcsEmployeeDataTable EcsEmpTable =
                EcsEpmloyeeAPI.GetEcsEmployeeByEmpEmail(mu.Email);

            string imgfile = ImageHelper.SaveImgPhotointoRealFolder("UploadEmpImg",
               Globals.Settings.UploadInfo.EmpImgAttachFolder);

            EcsSiteDS.EcsEmployeeRow EcsEmpRow = EcsEmpTable[0];
            if (!String.IsNullOrEmpty(imgfile))
            {
                if (!EcsEmpRow.IsEcsemp_PicturepathNull())
                {
                    System.IO.File.Delete(
                        HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                         + EcsEmpRow.Ecsemp_Picturepath));
                }
            }
            string JionCode = Convert.ToDateTime(txt_EmpSJion.TextBoxData).Year.ToString() +
                txt_EmpJionCode.TextBoxData;
           

            EcsEpmloyeeAPI.UpdateEcsEmployee(txt_EmpNameAr.TextBoxData, ddlEmpGender.SelectedItem.Value,
                DateTime.Parse(txt_EmpBirthDate.TextBoxData),
                txt_EmpAddressEg.TextBoxData, txt_EmpNameEn.TextBoxData, txt_EmpAddressEg.TextBoxData,
                txt_EmpPhoneEg.TextBoxData, txt_EmpEmail.TextBoxData, txt_EmpMobileEg.TextBoxData,
                DateTime.Parse(txt_EmpSJion.TextBoxData), JionCode, EmpEJionDate,
                txt_EmpPhoneOff.TextBoxData,
                txt_EmpMobileOff.TextBoxData, txt_EmpAddressOff.TextBoxData, string.Empty,
                imgfile, int.Parse(ddlEmpTypes.SelectedItem.Value), EmpID);


            imgPhoto.ImageUrl = ImageHelper.SetImgPhotoPath
                      (imgfile, null, PhotoSize.Small);
            return EmpID;
        }
        else
        {
            DateTime? EmpEJion;
            if (!string.IsNullOrEmpty(txt_EmpEJion.TextBoxData))
            {
                EmpEJion = Convert.ToDateTime(txt_EmpEJion.TextBoxData);
            }
            else
            {
                EmpEJion = null;
            }
            string JionCode = Convert.ToDateTime(txt_EmpSJion.TextBoxData).Year.ToString() + 
                txt_EmpJionCode.TextBoxData;

            string imgfile = ImageHelper.SaveImgPhotointoRealFolder("UploadEmpImg",
            Globals.Settings.UploadInfo.EmpImgAttachFolder);

            EmpID = EcsEpmloyeeAPI.AddEcsEmployee(txt_EmpNameAr.TextBoxData, ddlEmpGender.SelectedItem.Value,
                Convert.ToDateTime(txt_EmpBirthDate.TextBoxData),
                txt_EmpAddressEg.TextBoxData, txt_EmpNameEn.TextBoxData, txt_EmpAddressEg.TextBoxData,
                txt_EmpPhoneEg.TextBoxData, txt_EmpEmail.TextBoxData, txt_EmpMobileEg.TextBoxData,
                Convert.ToDateTime(txt_EmpSJion.TextBoxData), JionCode,
                EmpEJion,
                txt_EmpPhoneOff.TextBoxData,
                txt_EmpMobileOff.TextBoxData, txt_EmpAddressOff.TextBoxData, string.Empty,
                imgfile, Convert.ToInt32(ddlEmpTypes.SelectedItem.Value));
            return EmpID;
        }        
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "InsertNoData")
        {
            EmpQualfDescNoData = (GridView1.Controls[0].
                Controls[0].FindControl("txt_EmpQualfDescNoData") as UControlsBase_TextBoxCustom).TextBoxData;

            EmpQualfDateNoData = DateTime.Parse((GridView1.Controls[0].
                Controls[0].FindControl("txt_EmpQualfDateNoData") as UControlsBase_TextBoxCustom).TextBoxData);

            QualfDataSource.Insert();
        }
        else if (e.CommandName == "Insert")
        {
            EmpQualfDescNoData = (GridView1.FooterRow.FindControl("txt_EmpQualfDesc")
                as UControlsBase_TextBoxCustom).TextBoxData;
            EmpQualfDateNoData = DateTime.Parse((GridView1.FooterRow.FindControl("txt_EmpQualfDate")
                as UControlsBase_TextBoxCustom).TextBoxData);
            QualfDataSource.Insert();
        }
        //GridView1.Controls[0].Controls[0].FindControl("NoDataFirstName");
    }
    protected void QualfDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        e.InputParameters["EmpQualf_Desc"] = EmpQualfDescNoData;
        e.InputParameters["EmpQualf_Date"] = EmpQualfDateNoData;
        e.InputParameters["Ecsemp_id"] = EmpID;
    }
}
